﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using FastTravel.BusinessLogic;
using FastTravel.DB;
using FastTravel.DB.Helpers;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using Image = iTextSharp.text.Image;

namespace FastTravel.IntranetWeb
{
	public partial class CustomerReport : BasePage
	{
		public int QueryString(string paramName, int defaultValue)
		{
			int value;
			if (!int.TryParse(Request.QueryString[paramName], out value))
				return defaultValue;
			return value;
		}
		
		protected void Page_Load(object sender, EventArgs e)
		{
			int customerId = QueryString("id", 0);

			Customer customer = CustomerManager.Get(customerId);


			var ms = new MemoryStream();
			var document = new Document(PageSize.A4);
			PdfWriter writer = PdfWriter.GetInstance(document, ms);

			document.Open();

			Image headerlogo = Image.GetInstance(MapPath(reportLogo));
			headerlogo.Alignment = Image.TEXTWRAP | Image.RIGHT_ALIGN;
			document.Add(headerlogo);

			//Шрифты
			BaseFont baseFont = BaseFont.CreateFont(MapPath(reportFont), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			Font date = new Font(baseFont, 8, Font.NORMAL);
			Font text = new Font(baseFont, 10, Font.NORMAL);
			Font textBold = new Font(baseFont, 10, Font.BOLD);
			Font title = new Font(baseFont, 16, Font.BOLD);

			document.Add(new Paragraph("ОТЧЕТ О КЛИЕНТЕ \n",title));
			document.Add(new Paragraph("Компания: " + WebConfigurationManager.AppSettings.Get("Companyname"), date));
			document.Add(new Paragraph("Дата регистрации: " + customer.AddedDate, date));
			document.Add(new Paragraph("Дата последнего редактирования: " + customer.ModifyDate.ToString(), date));
			document.Add(new Paragraph("Дата печати: " + DateTime.Now + "\n\n", date));

			document.Add(new Paragraph(@"КЛИЕНТ", textBold));
			document.Add(new Paragraph(@"Ф.И.О.: " + customer.LastName + " " + customer.FirstName + " " + customer.MiddleName, text));
			document.Add(new Paragraph(@"Дата рождения: " + customer.BirthDate.ToShortDateString() + "\n", text));
			document.Add(new Paragraph(@" ", text));
			document.Add(new Paragraph(@"КОНТАКТНАЯ ИНФОРМАЦИЯ", textBold));
			document.Add(new Paragraph(@"Адрес1.: " + customer.AddressLine1, text));
			document.Add(new Paragraph(@"Адрес2.: " + customer.AddressLine2, text));
			document.Add(new Paragraph(@"Мобильный тел.: " + customer.PhoneMobile, text));
			document.Add(new Paragraph(@"Домашний тел.: " + customer.PhoneHome, text));
			document.Add(new Paragraph(@"Рабочий тел.:" + customer.PhoneWork, text));
			document.Add(new Paragraph(@"Email: " + customer.Email, text));
			document.Add(new Paragraph(@" ", text));
			document.Add(new Paragraph(@"ПАСПОРТНЫЕ ДАННЫЕ", textBold));
			document.Add(new Paragraph(@"Номер: " + customer.Passport.Number, text));
			document.Add(new Paragraph(@"Кем выдан: " + customer.Passport.Authority, text));
			document.Add(new Paragraph(@"Дата выдачи: " + customer.Passport.IssueDate.ToShortDateString(), text));
			document.Add(new Paragraph(@"Действует до: " + customer.Passport.ExpiryDate.ToShortDateString(), text));
			document.Add(new Paragraph(@" ", text));
			document.Add(new Paragraph(@"ДОПОЛНИТЕЛЬНО", textBold));
			document.Add(new Paragraph(@"Место работы: " + customer.JobPlacement, text));
			document.Add(new Paragraph(@"Профессия: " + customer.Profession, text));
			document.Add(new Paragraph(@"Семейное положение: " + EnumMetadataConverter.GetStringValue(customer.MarritalStatus), text));

			document.Close();

			//close the document
			Response.Clear();
			Response.ClearContent();
			Response.ClearHeaders();
			Response.ContentType = "application/pdf";
			byte[] b = ms.ToArray();
			int len = b.Length;
			Response.OutputStream.Write(b, 0, len);
			Response.OutputStream.Flush();
			Response.OutputStream.Close();
			ms.Close();

		}
	}
}